require "UIStepper"
YuanZhenScene={}
YuanZhenSceneFunc={}
YuanZhenSceneFunc.__index = YuanZhenSceneFunc
setmetatable(YuanZhenScene, YuanZhenSceneFunc)

YuanZhenSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, YuanZhenSceneFunc)
  l.node = CCBReader:load("YuanZhenScene.ccbi")
  print("YuanZhenScene.getNode", l.node)
  YuanZhenScene.instance = l
  l:init() 
  return l 
end

YuanZhenSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(YuanZhenScene.getNode().node)
  print("YuanZhenScene.getScene", sc)
  return sc
end

YuanZhenSceneFunc.init = function(self)
  setObjectExtTable(self.node:getChildByTag(1111), self)
  setObjectExtTable(self.node:getChildByTag(1022), self)
  setObjectExtTable(self.node:getChildByTag(1023), self)
  setObjectExtTable(self.node:getChildByTag(1024), self)
  setObjectExtTable(self.node:getChildByTag(1025), self)
  local bl = TypeCast(self.node:getChildByTag(1020), "CCButton")
  local br = TypeCast(self.node:getChildByTag(1021), "CCButton")
  self.uiStepper = UIStepper:create(bl, br, "onChanged", self)
 
  local curConId = UserData.userinfo.host.triggerHash.conId
  local count = math.floor((curConId-1)/10) + 1
  self.uiStepper:setCount(count)
  self.uiStepper:setIndex(count-1)
  
 
  --local index = math.floor((curConId-1)/10)
  --local index1 = curConId%10 
  --CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile(string.format("%s.plist", name))
  --local sp = CCSprite:createWithSpriteFrameName(string.format("%s_idle_0001.png", name))
  --self.curIndex = math.floor((curConId-1)/10)
  self:setState() 
end

YuanZhenSceneFunc.setState = function(self)
  local tmp = string.format("第%d战场", UserData.userinfo.host.triggerHash.conId)
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(tmp)
  tmp = tostring(UserData.userinfo.host.triggerHash.conSc)
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setString(tmp)

  if self.nodesNeedClean~=nil then
    for k,v in pairs(self.nodesNeedClean) do
      self.node:removeChild(v, true)
    end
  end
  self.nodesNeedClean = {}
  for i=0,9 do
    local tmp = self.uiStepper.index*10+i+1
    local sp = TypeCast(self.node:getChildByTag(1030+i), "CCSprite")
    local x,y = sp:getPosition() 
    if tmp < UserData.userinfo.host.triggerHash.conId then
      sp:setVisible(true)
      sp:stopAllActions() 
      local ac = ActionUtil.createAction("qizi.plist", 0.05, "")
      sp:runAction(CCRepeatForever:create(ac))
    elseif tmp==UserData.userinfo.host.triggerHash.conId then
      sp:setVisible(false)
      local imgBK = CCSprite:create("conquer_img1.png")
      imgBK:setPosition(ccp(x-15, y+10))
      self.node:addChild(imgBK)
      self.nodesNeedClean[#self.nodesNeedClean+1] = imgBK 
      local headImg = CCSprite:create(nameFromPath(UserData.userinfo.host.headpic))
      headImg:setPosition(ccp(x-15, y+20))
      headImg:setScale(0.5)
      self.node:addChild(headImg)
      self.nodesNeedClean[#self.nodesNeedClean+1] = headImg 
    else  
      sp:setVisible(false)  
    end
    local label = CCLabelTTF:create(string.format("第%d战场部队", tmp), "Arial", 20)
    label:setPosition(ccp(x-25, y-45))
    self.node:addChild(label)
    label:setColor(ccc3(187,247,63))
    self.nodesNeedClean[#self.nodesNeedClean+1] = label
  end
end

YuanZhenSceneFunc.onChanged = function(self, index)
  print("YuanZhenSceneFunc.onChanged", index)
  self:setState()
end

YuanZhenSceneFunc.onBeginAttackButton = function(self, node, event)
  if UserData.userinfo.host.soldiers>0 then
    NetworkRequest.getYuanZhengBattle(UserData.userinfo.host.triggerHash.conId, "onBattleComplete", self)
    --setTouchEnabledAll(self.node, false)
    LoadScene:showLoadingScene()
  else
    showMessage("兵力不足，请补充兵力！", ccc3(255,255,0))
  end
end
--[[
YuanZhenSceneFunc.showZhanDouView = function(self, data)
  FightData.load(data)
  local l = WarScene.getNode()
  l.zhandouScene = self
  self.node:getParent():addChild(l.node)
  self.warScene = l
end
YuanZhenSceneFunc.showWinWnd = function(self, exp, cash, eval)
  print("ZhanDouSceneFunc.showWinWnd", exp, cash, eval)

  local layer = CCBReader:load("WarWinScene.ccbi")
  local lb = layer:getChildByTag(1002)
  TypeCast(lb, "CCLabelTTF"):setString(string.format("您击败了第%d战场部队", UserData.userinfo.host.triggerHash.conId))
  local lb = layer:getChildByTag(1005)
  TypeCast(lb, "CCLabelTTF"):setString(tostring(cash))
  local lb = layer:getChildByTag(1006)
  TypeCast(lb, "CCLabelTTF"):setString(tostring(exp))

  print("ZhanDouSceneFunc.showWinWnd", FightData.json.data.drops)
  if FightData.json.data.drops~=nil then
    for k,v in pairs(FightData.json.data.drops) do
      local itemInfo = UserData.config.models.Item[v.id]
      local imgName = nameFromPath(itemInfo.asset)
      print("ZhanDouSceneFunc.showWinWnd", imgName, 2000+k)
      addSpriteFromBK(layer, 2000+k, imgName)
    end
  end

  setObjectExtTable(layer:getChildByTag(1111), self)
  self.node:getParent():addChild(layer)
end
]]
YuanZhenSceneFunc.onBattleComplete = function(self, data)
  print("YuanZhenSceneFunc.onBattleComplete", data)
  local res = cjson.decode(data)
  if res.ret~=0 then
    showMessage(res.msg, ccc3(255,255,0))
    setTouchEnabledAll(self.node, true)
  else
    NetworkRequest.syncData(res)
    --self:showZhanDouView(data)
    MainScene.scene:removeChild(self.node, true)
    local name = string.format("第%d战场部队", UserData.userinfo.host.triggerHash.conId)
    local war = WarScene:getNode(name, self, res)
    MainScene.scene:addChild(war.node)
  end
  LoadScene:removeLoadingScene()
end
YuanZhenSceneFunc.fightEnd = function(self)
  --self:setState()
  MainScene.scene:addChild(YuanZhenScene:getNode().node)
end
--[[
YuanZhenSceneFunc.onCloseWarWinScene = function(self, node, event)
  print("YuanZhenSceneFunc.onCloseWarWinScene")
  node:getParent():getParent():removeChild(node:getParent(), true)
  self.node:getParent():removeChild(self.warScene.node, true)
  setTouchEnabledAll(self.node, true)
  self:setState()
end
]]
YuanZhenSceneFunc.onRefreshButton = function(self, node, event)
  print("YuanZhenSceneFunc.onRefreshButton", UserData.userinfo.host.counter.conquerFreeFlush, UserData.userinfo.host.counter.conquerExtraFlush)
  if UserData.userinfo.host.counter.conquerFreeFlush<1 then
    local title = "重置远征战场"
    local msg = string.format("是否重置进度到当前地图的第%d关？每天第一次免费", self.uiStepper.index*10+1)
    self.dlg = Dialog:create(title, msg, self)
    self.node:getParent():addChild(self.dlg.node)  
    MainScene.scene:removeChild(YuanZhenScene.instance.node, true)
  elseif UserData.userinfo.host.counter.conquerExtraFlush<1 then
    self.dlg = {}
    self.dlg.node = CCBReader:load("YuanZhenResetDialog.ccbi") 
    local msg = string.format("是否消耗1个远征令或50军饷，重置进度到\n当前地图的第%d关？", self.uiStepper.index*10+1)
    TypeCast(self.dlg.node:getChildByTag(1011), "CCLabelTTF"):setString(msg)
    TypeCast(self.dlg.node:getChildByTag(1012), "CCLabelTTF"):setString(string.format("%d/%d", 1, ItemManager:getItemCount(102)))
    TypeCast(self.dlg.node:getChildByTag(1013), "CCLabelTTF"):setString(string.format("%d/%d", 50, UserData.userinfo.host.coin))
    setObjectExtTable(self.dlg.node:getChildByTag(1020), self)
    setObjectExtTable(self.dlg.node:getChildByTag(1021), self)
    setObjectExtTable(self.dlg.node:getChildByTag(1111), self)
    self.node:getParent():addChild(self.dlg.node)  
    MainScene.scene:removeChild(YuanZhenScene.instance.node, true)
  else
    showMessage("您今天所有的重置机会已经全部使用完毕了", ccc3(255,0,0))
  end 
end
YuanZhenSceneFunc.onConfirm = function(self, dlg, confirm)
  if confirm==false then
    MainScene.scene:removeChild(dlg.node, true)
    MainScene.scene:addChild(YuanZhenScene:getNode().node)
  else
    --NetworkRequest.resetAndRefreshGeneral(self.jiasuID, self.cost, "onJiaSuComplete", self)
    --setTouchEnabledAll(dlg.node, false)
    LoadScene:showLoadingScene()
    NetworkRequest.refreshYuanZheng(self.uiStepper.index*10+1, "free", "onRefreshYuanZhen", self)
    self.flushType = 1
  end
  --[[
  self.node:setVisible(true)
  self.node:getParent():removeChild(self.dialog.node, true)
  setTouchEnabledAll(self.node, true)  
  NetworkRequest.refreshYuanZheng(self.uiStepper.index*10+1, "onRefreshYuanZhen", self)
  ]]
end
YuanZhenSceneFunc.onRefreshYuanZhen = function(self, data)
  print("YuanZhenSceneFunc.onRefreshYuanZhen", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    if self.flushType==1 then
      UserData.userinfo.host.counter.conquerFreeFlush = 1
    elseif self.flushType==2 then
      UserData.userinfo.host.counter.conquerExtraFlush = 1
    end
    --MainScene.scene:addChild(YuanZhenScene.getNode().node)
    --UserData.userinfo.host.triggerHash.conId = self.uiStepper.index*10+1 
    --[[
    local curConId = UserData.userinfo.host.triggerHash.conId
    local count = math.floor((curConId-1)/10) + 1
    self.uiStepper:setCount(count)
    self.uiStepper:setIndex(count-1)
    self:setState()
    ]]
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  if self.dlg~=nil then
    MainScene.scene:removeChild(self.dlg.node, true)
  end
  MainScene.scene:addChild(YuanZhenScene.getNode().node)
end

YuanZhenSceneFunc.onJunXiangButton = function(self, node, event)
  NetworkRequest.refreshYuanZheng(self.uiStepper.index*10+1, "coin", "onRefreshYuanZhen", self)
  self.flushType = 2
  --setTouchEnabledAll(self.dlg.node, false)
  LoadScene:showLoadingScene()
end
YuanZhenSceneFunc.onYuanZhengLingButton = function(self, node, event)
  NetworkRequest.refreshYuanZheng(self.uiStepper.index*10+1, "yuanzhengling", "onRefreshYuanZhen", self)
  self.flushType = 2
  --setTouchEnabledAll(self.dlg.node, false)
  LoadScene:showLoadingScene()
end
--YuanZhenSceneFunc.onRefreshYuanZhen = function(self, data)
YuanZhenSceneFunc.onYuanZhenShopButton = function(self, node, event)
  MainScene.scene:removeChild(self.node, true)
  MainScene.scene:addChild(YuanZhenShopScene:getNode().node)
end

YuanZhenSceneFunc.onRankButton = function(self, node, event)
  NetworkRequest.seeTopN("onSeeTopNComplete", self)
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene()
end
YuanZhenSceneFunc.onSeeTopNComplete = function(self, data)
  print("YuanZhenShopSceneFunc.onSeeTopNComplete", data)
  local res = cjson.decode(data)
  if res.ret==0 then
    self.rankWnd = CCBReader:load("YuanZhenRankScene.ccbi")
    setObjectExtTable(self.rankWnd:getChildByTag(1111), self)
    local items = CCArray:create()
    local ranks = res.data.topN 
    for i=1,#ranks do
      local v = ranks[#ranks-i+1]
      local item = CCBReader:load("DailyQuestRankItem.ccbi")
      TypeCast(item:getChildByTag(1010), "CCLabelTTF"):setString(tostring(v.rank))
      TypeCast(item:getChildByTag(1011), "CCLabelTTF"):setString(v.name)
      TypeCast(item:getChildByTag(1012), "CCLabelTTF"):setString(string.format("第%d战场", v.conMaxId))
      items:addObject(item)
      item:setContentSize(CCSizeMake(740,50))
    end

    local tableview = CCTableView:create(1, CCSizeMake(750, 320), items)
    tableview:setPosition(100, 115)
    self.rankWnd:addChild(tableview)
    self.rankTable = tableview
    MainScene.scene:removeChild(self.node, true)
    MainScene.scene:addChild(self.rankWnd)
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  --setTouchEnabledAll(self.node, true)
  LoadScene:removeLoadingScene()
end
YuanZhenSceneFunc.onCloseRankButton = function(self, node, event)
  MainScene.scene:removeChild(self.rankWnd, true)
  MainScene.scene:addChild(YuanZhenScene:getNode().node)
end
YuanZhenSceneFunc.onCloseDialogButton = function(self, node, event)
  MainScene.scene:removeChild(self.dlg.node, true)
  MainScene.scene:addChild(YuanZhenScene:getNode().node) 
end

YuanZhenSceneFunc.onClose = function(self, node, event)
  print("onCloseZhiHuiBu")
  MainScene.scene:removeChild(node:getParent(), true)
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
  YuanZhenScene.instance = nil
end

